package com.zq.system.config.db;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import java.sql.SQLException;

/**
 * Redis数据源配置
 */
@Configuration
@PropertySource({"classpath:db.properties"})
public class DataConfig implements EnvironmentAware{
    @Autowired
    private Environment environment;

    @Bean(name = "druidDataSource", destroyMethod = "close")
    public DruidDataSource dataSource() throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(environment.getProperty("db.url"));
        dataSource.setUsername(environment.getProperty("db.username"));
        dataSource.setPassword(environment.getProperty("db.password"));
        dataSource.setInitialSize(Integer.parseInt(environment.getProperty("db.initialSize")));
        dataSource.setMinIdle(Integer.parseInt(environment.getProperty("db.minIdle")));
        dataSource.setMaxActive(Integer.parseInt(environment.getProperty("db.maxActive")));
        dataSource.setMaxWait(Long.parseLong(environment.getProperty("db.maxWait")));
        dataSource.setTimeBetweenEvictionRunsMillis(Long.parseLong(environment.getProperty("db.timeBetweenEvictionRunsMillis")));
        dataSource.setMinEvictableIdleTimeMillis(Long.parseLong(environment.getProperty("db.minEvictableIdleTimeMillis")));
        dataSource.setValidationQuery(environment.getProperty("db.validationQuery"));
        dataSource.setTestWhileIdle(Boolean.parseBoolean(environment.getProperty("db.testWhileIdle")));
        dataSource.setTestOnBorrow(Boolean.parseBoolean(environment.getProperty("db.testOnBorrow")));
        dataSource.setTestOnReturn(Boolean.parseBoolean(environment.getProperty("db.testOnReturn")));
        dataSource.setPoolPreparedStatements(Boolean.parseBoolean(environment.getProperty("db.poolPreparedStatements")));
        dataSource.setMaxOpenPreparedStatements(Integer.parseInt(environment.getProperty("db.maxOpenPreparedStatements")));
        dataSource.setConnectionProperties(environment.getProperty("db.connectionProperties"));
        dataSource.setFilters(environment.getProperty("db.filters"));
        return dataSource;
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactoryBean sqlSessionFactory() throws SQLException {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(this.dataSource());
        sqlSessionFactoryBean.setTypeAliasesPackage("com.zq.*.storage.db.po;com.zq.*.query;");
        return sqlSessionFactoryBean;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
        configurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        configurer.setBasePackage("com.zq.*.storage.db,;");
        return configurer;
    }

    @Bean(name = "transactionManager")
    public DataSourceTransactionManager transactionManager() throws SQLException {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(this.dataSource());
        return transactionManager;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment=environment;
    }
}
